package service

import (
	"html/template"
	"net/http"
	"strconv"

	"github.com/unrolled/render"
)

func apiTestHandler(formatter *render.Render) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		formatter.JSON(w, http.StatusOK, struct {
			ID int
			Content string
		}{ID: 18342063,Content:" hello!!!"})
	}
}

func homeHandler(formatter *render.Render) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		template := template.Must(template.New("index.html").ParseFiles("./templates/index.html"))
		_ = template.Execute(w, struct {
			ID      string
			Content string
		}{ID: "18342063", Content: "Bonjour!"})
	}
}

func checkform(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	username := template.HTMLEscapeString(r.Form.Get("username"))
	password := template.HTMLEscapeString(r.Form.Get("password"))
	num1 :=  template.HTMLEscapeString(r.Form.Get("num1"))
	num2 :=  template.HTMLEscapeString(r.Form.Get("num2"))
	n1,_ := strconv.Atoi(num1)
	n2,_ := strconv.Atoi(num2)
	sum := strconv.Itoa(n1+n2)
	t := template.Must(template.New("signin.html").ParseFiles("./templates/signin.html"))
	err := t.Execute(w, struct {
		Username string
		Password string
		Num1 string
		Num2 string
		Sum string
	}{Username: username, Password: password, Num1: num1,Num2:num2, Sum:sum})
	if err != nil {
		panic(err)
	}
}
